
import ConcurrentQueue from "../commons/concurrentQueue"
import request from '@/utils/request'


const fileName = '/api-archive'

//影像导入
export const imgUpload = function({data,front_id,system_id,institution_id,onUploadProgress,cancelToken}){

  return request({
    url:fileName+`/Instance/upload?front_id=${front_id}&system_id=${system_id}&institution_id=${institution_id}`,
    method:'post',
    data:data,
    headers:{'Content-Type': 'multipart/form-data'},
    onUploadProgress,
    cancelToken:cancelToken.token
  })

}

export function  getUuid () {
  let _time = new Date().getTime();
  let guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = (_time + Math.random() * 16) % 16 | 0;
    _time = Math.floor(_time / 16);
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  
  return guid;
}


export class UploadImageFile{
  constructor(params){
    this.system_id = params.system_id
    this.institution_id = params.institution_id
    this.front_id = params.front_id
    this.uploadQueue = new ConcurrentQueue({maxConcurrentCount:6})
    this.successQueues = []
    this.failQueues = []
    this.files = params.files

  }


  setUploadQueue(){

    if(this.files.length === 0)
    {
        return
    }
    for(let i = 0 ; i < this.files.length;i ++ )
    {
      
        var file = this.files[i]
        //是否有相同文件----允许相同文件导入
        // let sameFiles = this.uploadQueue.taskQueue.filter(ele => ele.fileName === file.name && ele.fileSize === file.size)
        // if(sameFiles.length>0){
        //     this.callback.sameCallback&&this.callback.sameCallback(file)
        //     break //不再加入队列
        // }

        let fd = new FormData();
        fd.append('file',file);

        let data = {
            name:file.name+`_index_${i}`,
            fileName:file.name,
            file:file,
            fileSize:file.size,
            fn:imgUpload,
            params:{data: fd,system_id:this.system_id,institution_id:this.institution_id,front_id:this.front_id}
        }
        this.uploadQueue.addTask(data)
    }
    this.uploadQueue.callback = {
      complete:this.callback.complete,
      cancelback:this.callback.cancelback

    }
    
    this.startUpload()
  }


  cancel(){
    
    this.uploadQueue.cancel();

  }
  startUpload(){
      this.uploadQueue.startQueue();
  }





}
